package com.ma.home;

import android.content.Intent;
import android.os.Bundle;

import com.ma.R;
import com.ma.fees.FeesRecordActivity;
import com.ma.reportForm.ReportFormActivity;
import com.ma.settings.SettingsActivity;
import com.ma.workorder.InProcessActivity;
import com.ma.workorder.PendingActivity;
import com.ma.workorder.ProcessedActivity;
import com.ma.workorder.RepairActivity;
import com.ma.workorder.TimeoutOrderActivity;

import java.util.ArrayList;
import java.util.List;

import base.BaseActivity;
import bean.LoginBean;
import database.dao.DBManager;
import database.table.Zyda;
import lomasky.xui.BaseViewHolderForListView;
import lomasky.xui.CommonAdapter;
import lomasky.xui.NoScrollGridView;
import utils.ConstantUtil;

public class HomeActivity extends BaseActivity {

    private NoScrollGridView iconGroup;
    private NoScrollGridView iconGroup1;
    private NoScrollGridView iconGroup2;
    private NoScrollGridView iconGroup3;
    LoginBean loginBean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        loginBean = (LoginBean) getIntent().getSerializableExtra(ConstantUtil.DATA1);
        iconGroup = (NoScrollGridView) findViewById(R.id.icon_group);
        iconGroup1 = (NoScrollGridView) findViewById(R.id.icon_group1);
        iconGroup2 = (NoScrollGridView) findViewById(R.id.icon_group2);
        iconGroup3 = (NoScrollGridView) findViewById(R.id.icon_group3);
        initView();
        initView1();
        initView2();
        initView3();
    }

    private void initView() {
        List<IconText> list = new ArrayList<>();
        list.add(new IconText("物业收费", R.mipmap.wu_ye_shou_fei));
        list.add(new IconText("缴费记录", R.mipmap.jiao_fei_ji_lu));
        list.add(new IconText("", R.drawable.transparent));
        list.add(new IconText("", R.drawable.transparent));
        CommonAdapter<IconText> adapter = new CommonAdapter<IconText>(this, list, R.layout.home_icon_text) {
            @Override
            public void convert(BaseViewHolderForListView helper, IconText item, int position) {
                helper.setText(R.id.textView, item.text);
                helper.setImage(R.id.imageView, item.icon);

            }
        };

        iconGroup.setOnItemClickListener((adapterView, view, i, l) -> {
            if (i == 0) {
//               new DownloadFeesDialog(this).show();
                startActivity(ChooseRoomActivity.class);
            } else if (i == 1) {
//                new UploadFeesDialog(this).show();
                startActivity(FeesRecordActivity.class);
            }


        });

        iconGroup.setAdapter(adapter);

    }

    private void initView2() {
        List<IconText> list = new ArrayList<>();
        list.add(new IconText("超期工单", R.mipmap.chao_qi_gong_dan));
        list.add(new IconText("报表管理", R.mipmap.bao_biao_guan_li));
        list.add(new IconText("", R.drawable.transparent));
        list.add(new IconText("", R.drawable.transparent));
        CommonAdapter<IconText> iconTextCommonAdapter = new CommonAdapter<IconText>(this, list, R.layout.home_icon_text) {
            @Override
            public void convert(BaseViewHolderForListView helper, IconText item, int position) {
                helper.setText(R.id.textView, item.text);
                helper.setImage(R.id.imageView, item.icon);

            }
        };

        iconGroup2.setAdapter(iconTextCommonAdapter);
        iconGroup2.setOnItemClickListener((adapterView, view, i, l) -> {
            if (i == 0) {
                startActivity(TimeoutOrderActivity.class);
            } else if (i == 1) {
                startActivity(ReportFormActivity.class);
            }


        });


    }

    private void initView1() {

        List<IconText> list = new ArrayList<>();
        list.add(new IconText("待处理工单", R.mipmap.dai_chu_gong_dan));
        list.add(new IconText("处理中工单", R.mipmap.chu_li_zhong_gong_dan));
        list.add(new IconText("已完成工单", R.mipmap.yi_wan_cheng_gong_dan));
        list.add(new IconText("报事报修", R.mipmap.bao_shi_bao_xui));

        CommonAdapter<IconText> iconTextCommonAdapter = new CommonAdapter<IconText>(this, list, R.layout.home_icon_text) {
            @Override
            public void convert(BaseViewHolderForListView helper, IconText item, int position) {
                helper.setText(R.id.textView, item.text);
                helper.setImage(R.id.imageView, item.icon);

            }
        };

        iconGroup1.setAdapter(iconTextCommonAdapter);
        iconGroup1.setOnItemClickListener((adapterView, view, i, l) -> {
            if (i == 0) {
                Intent intent = new Intent(this, PendingActivity.class);
                intent.putExtra(ConstantUtil.DATA1, loginBean);
                startActivity(intent);


            } else if (i == 1) {
                startActivity(InProcessActivity.class);
            } else if (i == 2) {
                startActivity(ProcessedActivity.class);
            } else if (i == 3) {
                Intent intent = new Intent(this, RepairActivity.class);
                intent.putExtra(ConstantUtil.DATA1, loginBean);
                startActivity(intent);
            }

        });
    }

    private void initView3() {
        List<IconText> list = new ArrayList<>();
        list.add(new IconText("系统设置", R.mipmap.xi_tong_she_zhi));
        list.add(new IconText("数据同步", R.mipmap.shu_ju_tong_bu));
        list.add(new IconText("", R.drawable.transparent));
        list.add(new IconText("", R.drawable.transparent));
        CommonAdapter<IconText> adapter = new CommonAdapter<IconText>(this, list, R.layout.home_icon_text) {
            @Override
            public void convert(BaseViewHolderForListView helper, IconText item, int position) {
                helper.setText(R.id.textView, item.text);
                helper.setImage(R.id.imageView, item.icon);

            }
        };

        iconGroup3.setOnItemClickListener((adapterView, view, i, l) -> {
            if (i == 0) {
                startActivity(SettingsActivity.class);
            } else if (i == 1) {
//                DataSyncDialog dialog=  new DataSyncDialog(this);
//                dialog.show();
                Intent intent = new Intent(this, SycnActivity.class);
                intent.putExtra(ConstantUtil.DATA1, loginBean);
                startActivity(intent);

            }

        });

        iconGroup3.setAdapter(adapter);
    }



    class IconText {
        public String text;
        public int icon;

        public IconText(String text, int icon) {
            this.text = text;
            this.icon = icon;
        }
    }
}
